package com.example.webdemo.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.logging.Log;
import org.slf4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Date 2021/6/24 10:03
 * @Created by wangle
 * @Description 登录检查
 */
@Slf4j
public class mtincerceptor  implements HandlerInterceptor {

    //目标方法执行前

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri= request.getRequestURI();
        log.info("拦截器拦截的连接{}",uri);


        HttpSession session=request.getSession();
        Object  aa= session.getAttribute("loguser");

        if (aa!=null){
            return true;
        }




        //拦截住，跳转到登录页
//        session.setAttribute("msg","请先登录");
//        response.sendRedirect("/");
        request.setAttribute("msg","请先登录");
        request.getRequestDispatcher("/").forward(request,response);

        return false;


    }

    //目标方法执行完后
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }
//页面渲染之后
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
